<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/lovely.jpg" alt="">
            </div>
            <!-- model绑定表单数据 rules验证表单信息 ref绑定表单实例对象 -->
            <el-form label-width="100px" class="demo-ruleForm form-box" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" prefix-icon="iconfont icon-RectangleCopy
" v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" prefix-icon="iconfont icon-RectangleCopy1
" v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            loginForm: {
                username:'admin',
                password:'123456'
            },
            loginFormRules: {
                username:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 100, message: '长度至少3个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        resetLoginForm:function () {
            // 重置
            this.$refs.loginFormRef.resetFields();
        },
        login: function () {
            this.$refs.loginFormRef.validate(async valid => {
                // console.log(valid)
                if(!valid) return;
                let result = await this.$http.post("login",this.loginForm);
                let {data : res} =result;// 结构解系 取出result 里面的 data 取一个res的小名
                // console.log(result);
                if(res.meta.status != 200) return this.$message.error('登录失败哦~');
                this.$message.success('登录成功，小伙子！');
                // SessionStorage 关闭浏览器就会消失 LocalStorage 本地存储 要主动删除
                window.sessionStorage.setItem('token',res.data.token);
                // 跳转
                this.$router.push('/home');
            })
        }
    }
  }
</script>

lang=<style lang="less" scoped>
// scope 表示该样式只能用于当前的页面
.login_container {
    background-color: #2b4b6b;
    height: 100%;
}
.login_box {
    width: 480px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
    border-radius: 6px;
}
.avatar_box {
    width: 130px;
    height: 130px;
    border:2px solid pink;
    border-radius: 50%;
    padding: 10px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%); 
    img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: pink;
    }
}
.form-box {
        height: 200px;
        margin-top:100px;
        margin-right: 36px;
        .btns {
        display:flex;
        justify-content: flex-end;
        }
    }

</style>



